import router from "./router";
import { getToken } from "./composables/auth";
import { showFullLoading, hideFullLoading, toast } from "./composables/utils";


// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  // 显示loading
  showFullLoading();

  const token = getToken();

  if (!token && to.path != "/login") {
    toast("请先登录", "error");
    return next({ path: "/login" });
  }

  // 防止重复登录
  if (token && to.path == "/login") {
    toast("请勿重复登录", "error");
    return next({ path: from.path });
  }
  
  // 设值页面标题
  let title = (to.meta.title ? to.meta.title : "") + "-KMS后台";
  document.title = title;

  next();
});

// 全局后置守卫
router.afterEach((to, from) => hideFullLoading());
